<?php

namespace app\admin\model;

use think\Model;

class HeyInfo extends Model
{
    // 表名
    protected $name = 'hey_info';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'goods_update_text',
        'order_update_text',
        'express_update_text',
        'trans_switch_text',
        'update_switch_text',
    ];

    /**
     * 通过Adminid获取order config
     * @param  [type] $admin_id [description]
     * @return [type]           [description]
     */
    public static function getOrderConfig($admin_id){
        $info = self::where('admin_id',$admin_id)->value('order_config');
        if ($info) {
            return json_decode($info,true);
        }else{
            return false;
        }
    }
    /**
     * 通过Adminid获取payconfig
     * @param  [type] $admin_id [description]
     * @return [type]           [description]
     */
    public static function getPayConfig($admin_id){
        $info = self::where('admin_id',$admin_id)->value('pay_config');
        if ($info) {
            return json_decode($info,true);
        }else{
            return false;
        }
    }
    
    public function getTransSwitchList()
    {
        return ['0' => __('Update_switch 0'),'1' => __('Update_switch 1')];
    }

    public function getUpdateSwitchList()
    {
        return ['0' => __('Update_switch 0'),'1' => __('Update_switch 1')];
    }


    public function getGoodsUpdateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['goods_update']) ? $data['goods_update'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderUpdateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_update']) ? $data['order_update'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExpressUpdateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['express_update']) ? $data['express_update'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTransSwitchTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['trans_switch']) ? $data['trans_switch'] : '');
        $list = $this->getTransSwitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getUpdateSwitchTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['update_switch']) ? $data['update_switch'] : '');
        $list = $this->getUpdateSwitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setGoodsUpdateAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setOrderUpdateAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setExpressUpdateAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'RIGHT')->setEagerlyType(0);
    }
}
